package tzhomestudio.framework.implementation.main_service;

import tzhomestudio.framework.interfaces.config_service.IConfigService;
import tzhomestudio.framework.interfaces.log_service.ILogService;
import tzhomestudio.framework.implementation.config_service.FileConfigService;
import tzhomestudio.framework.implementation.log_service.LogService;

/**
 * This class contains links to all services.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class FrameworkMainService {
    // Core services
    protected static IConfigService configService;
    protected static ILogService logService;

    // Main data
    protected static boolean init = false;

    // Mutex
    protected static final Object generalMutex = new Object();

    /**
     * Executes initial preparations.
     */
    public static void executeInitialPreparations() {
        synchronized (generalMutex) {
            if (init) return;

            // execute preparations
            logService = new LogService();
            FrameworkMainServiceHelper.logger = logService.getLogger();

            configService = FileConfigService.getInstance();
            FrameworkMainServiceHelper.configService = configService;

            // update helper
            updateHelper();

            init = true;
        }
    }

    public static void updateHelper() {
        synchronized (generalMutex) {
            FrameworkMainServiceHelper.logger = logService.getLogger();
            FrameworkMainServiceHelper.windowMessenger = logService.getWindowMessenger();
            FrameworkMainServiceHelper.configService = configService;
        }
    }
}
